package game;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 * The player - a cute little robot with bombs
 * @author Simon
 *
 */
public class Player {
	public static final int D_UP = 0;
	public static final int D_RIGHT = 1;
	public static final int D_DOWN = 2;
	public static final int D_LEFT = 3;
	
	public static final int ANIM_DELAY = 3;
	
	public static final int[][] ANIM_FRAME = {
		{3,4,3,5},
		{9,10,9,11},
		{0,1,0,2},
		{6,7,6,8},
	};
	
	public static final int[] DEATH_ANIM_FRAME = {12, 13, 14, 15, 16, 17};
	
	public static final Rect COLLISION_RECT = new Rect(3, 3, 14, 14);
	
	Sprite gfx = null;
	
	float x, y;
	float speed;
	int animFrame;
	int animDelay;
	int direction;
	int bombRange;
	int maxBombs;
	int bombCount;
	int dead;
	int score;
	
	private void updateSprite() {
		gfx.setFrame(ANIM_FRAME[direction][animFrame]);
		gfx.setPosition((int)x + 10, (int)y + 5);
	}
	
	public Player() {
		try {
			gfx = new Sprite(Image.createImage("/gfx/robot_tile.png"), 20, 20);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		resetPosition();
		speed = 1.5f;
		bombRange = 1;
		maxBombs = 1;
		bombCount = 0;
		dead = 0;
		score = 0;
		updateSprite();
	}
	
	public void resetPosition() {
		x = 0;
		y = 0;
		animDelay = ANIM_DELAY;
		animFrame = 0;
		direction = D_DOWN;
	}
	
	public void input(Arena arena) {
		if (dead == 2) {
			animDelay++;
			if (animDelay > 20) {
				arena.gameOver();
			}
			return;
		}
		if (dead == 1) {
			animDelay--;
			if (animDelay <= 0) {
				animDelay = ANIM_DELAY;
				animFrame++;
				if (animFrame < DEATH_ANIM_FRAME.length) {
					gfx.setFrame(DEATH_ANIM_FRAME[animFrame]);
				} else {
					dead = 2;
				}
			}
			return;
		}
		boolean walk = true;
		if (LUMM2_Canvas.down) {
			direction = D_DOWN;
			y += speed;
		} else if (LUMM2_Canvas.up) {
			direction = D_UP;
			y -= speed;
		} else if (LUMM2_Canvas.left) {
			direction = D_LEFT;
			x -= speed;
		} else if (LUMM2_Canvas.right) {
			direction = D_RIGHT;
			x += speed;
		} else {
			walk = false;
		}
		if (arena.playerWallCollision(this)) {
			walk = false;
		}
		arena.playerPowerupCollision(this);
		arena.playerExitCollision(this);
		if (LUMM2_Canvas.fire) {
			if (placeBomb()) {
				arena.placeBomb(this);
			}
		}
		if (walk) {
			animDelay--;
			if (animDelay <= 0) {
				animDelay = ANIM_DELAY;
				animFrame++;
				animFrame %= 4;
			}
		} else {
			animFrame = 0;
		}
		updateSprite();
	}
	
	public void draw(Graphics g) {
		if (dead < 2) gfx.paint(g);
	}
	
	public int getBombStrength() {
		return bombRange;
	}
	
	public void kill() {
		if (dead == 0) {
			dead = 1;
			animFrame = 0;
			animDelay = ANIM_DELAY;
		}
	}
	
	public int getTileX() {
		return (((int)this.x) + Arena.TILESIZE/2) / Arena.TILESIZE;
	}
	
	public int getTileY() {
		return (((int)this.y) + Arena.TILESIZE/2) / Arena.TILESIZE;
	}
	
	public boolean placeBomb() {
		if (bombCount < maxBombs) {
			return true;
		}
		return false;
	}
	
	public void putBomb() {
		bombCount++;
	}
	
	public void returnBomb() {
		bombCount--;
	}

	public void eatPowerup(Powerup powerup) {
		switch (powerup.type) {
		case Powerup.T_BOMB_COUNT:
			this.maxBombs++;
			System.err.println("max bombs: " + this.maxBombs);
			break;
		case Powerup.T_BOMB_RANGE:
			this.bombRange++;
			System.err.println("bomb range: " + this.bombRange);
			break;
		}
		addScore(100);
	}
	
	public void addScore(int amount) {
		score += amount;
	}
	
	public int getScore() {
		return this.score;
	}
}
